<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  
  <title>
    BLE 连接 | 朝雨绫萧的博客
  </title>
  <meta content="width=device-width, initial-scale=1" name="viewport">
  <meta name="theme-color" content="#4184f3">
  
  
  <link href="/image/favicon.png" rel="icon" />
  

  
<link rel="stylesheet" href="/css/highlight.light.css">

  
<link rel="stylesheet" href="/css/prism-customize.css">

  
<link rel="stylesheet" href="/css/nav-icon.css">

  
<link rel="stylesheet" href="/css/waves.min.css">

  
<link rel="stylesheet" href="/css/jquery.tocify.css">

  
<link rel="stylesheet" href="/css/main.css">

  
<link rel="stylesheet" href="/css/nav-indicator.css">

  
<link rel="stylesheet" href="/css/twikoo.css">

  
<link rel="stylesheet" href="/css/search.css">

  
  

  

  </meta>
  </meta>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  
<script src="/js/comment.js"></script>

  
<script src="/js/copy.js"></script>

<meta name="generator" content="Hexo 5.4.0"></head>

	<body>
		<header>
			<!-- cover image or sth. -->
		</header>
		<div id="main" class="m-scene">
			
<div class="nav-wrapper" id="header">

    <div class="container">
        <nav>
            
            
            <div class="logo wave">
                <a href="/" id="logo">
                    朝雨绫萧的博客
                </a>
            </div>

            

            <div class="nav-toggle-icon" >
                <div class="material-hamburger">
                    <span>
                    </span>
                    <span>
                    </span>
                    <span>
                    </span>
                </div>
            </div>

            <div class="menu-wrapper">
                <div class="nav-indicator">
                </div>
                <ul class="menus">
                    
                     
                        <li>
                            <a class="wave " href="/">
                                首页
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/archives">
                                归档
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/about">
                                关于
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/message">
                                留言
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/search">
                                搜索
                            </a>
                        </li>
                     
                    
                   
                </ul>
            </div>

        </nav>
    </div>
</div>


				<div class="container content">
					<div class="scene_element scene_element--fadein">
						<div class="row">
  <div class="main main-article">
    <article>

      
        <header class="post-header no-cover">
      
        </header>

      <!-- 标题 -->
      <h1 class="post-title">
        BLE 连接
      </h1>

      <section class="post-info post-info-bottom">

        <!-- 发布时间 -->
        <span class="post-date">
          <span class="post-date-text">
            发布于: 2021-02-28
          </span>
        </span>
        
        <!-- 分类 -->
        
          <span class="post-category">分类:
            <a class="article-category-link" href="/categories/BLE/">BLE</a>
          </span>
        

        <!-- 标签 -->
        
          <span class="post-tags">
            <ul class="post-tag-list" itemprop="keywords"><li class="post-tag-list-item"><a class="post-tag-list-link" href="/tags/BLE/" rel="tag">BLE</a></li></ul>
          </span>
        

        <!-- 阅读量统计 -->
        

      </section>

      <!-- 正文 -->
      <section class="post-content">
        <h1 id="BLE-连接"><a href="#BLE-连接" class="headerlink" title="BLE 连接"></a>BLE 连接</h1><h2 id="连接过程介绍"><a href="#连接过程介绍" class="headerlink" title="连接过程介绍"></a>连接过程介绍</h2><ol>
<li>Advertiser 广播</li>
<li>Observer 接收到广播</li>
<li>在 Initiator 接收窗口时间内发送连接请求（CONN_REQ）</li>
<li>Advertiser 根据 connect_req 中信息做好连接准备</li>
<li>Central 发送同步包（发送同步包后，Central 认为连接成功）</li>
<li>Peripheral 回复 ACK</li>
</ol>
<blockquote>
<p>根据蓝牙spec规定，advertiser 发送完一个广播包之后 <strong>150us（T_IFS）</strong>，advertiser 必须开启一段时间的射频 Rx 窗口，以接收来自 observer 的数据包。</p>
</blockquote>
<h2 id="CONN-REQ数据包分析"><a href="#CONN-REQ数据包分析" class="headerlink" title="CONN_REQ数据包分析"></a>CONN_REQ数据包分析</h2><p>CONNECT_REQ PDU 的 Paylpad 如下：</p>
<table>
<thead>
<tr>
<th align="center">InitA</th>
<th align="center">AdvA</th>
<th align="center">LLData</th>
</tr>
</thead>
<tbody><tr>
<td align="center">6 byte</td>
<td align="center">6 byte</td>
<td align="center">22 byte</td>
</tr>
</tbody></table>
<p>LLData 域格式如下所示：</p>
<table>
<thead>
<tr>
<th align="center">Access Address</th>
<th align="center">CRC Init</th>
<th align="center">Win Size</th>
<th align="center">Win Offset</th>
<th align="center">interval</th>
<th align="center">Latency</th>
<th align="center">Timeout</th>
<th align="center">Channel Map</th>
<th align="center">Hop</th>
<th align="center">Sleep Clock Accuracy</th>
</tr>
</thead>
<tbody><tr>
<td align="center">4 byte</td>
<td align="center">3 byte</td>
<td align="center">1 byte</td>
<td align="center">2 byte</td>
<td align="center">2 byte</td>
<td align="center">2 byte</td>
<td align="center">2 byte</td>
<td align="center">5 byte</td>
<td align="center">5 bit</td>
<td align="center">3 bit</td>
</tr>
</tbody></table>
<p><strong>Access Address</strong>：区别同一物理信道下的不同设备，每次连接都会随机生成。</p>
<p><strong>CRC Init</strong>：用于CRC计算的一个初始值，由Link Layer随机生成。</p>
<p><strong>Win Size</strong>：全称是 transmitWindowSize，用于决定连接双方收发数据的时间窗口</p>
<p><strong>Win Offset</strong>：全称是 transmitWindowOffset，用于决定第一次连接的偏移</p>
<p><strong>interval</strong>：连接双方收发数据的间隔</p>
<p><strong>Latency</strong>：全称是connSlaveLatency，即空闲时 Peripheral 可以忽略的连接事件数量</p>
<p><strong>Timeout</strong>：连接超时时间</p>
<p><strong>Channel Map</strong>：用于标识当前使用和未使用的 Physical Channel。</p>
<p><strong>Hop</strong>：全称是hopIncrement，它和ChM一起决定了数据传输过程中的跳频算法</p>
<p><strong>Sleep Clock Accuracy</strong>：定义最差的Master睡眠时钟精度</p>

      </section>

    </article>
    
    


    <!-- 版权声明 -->
    <div class="post-copyright">
	<div>
		<strong>本文作者：</strong>
		<span>
			朝雨绫萧
		</span>
	</div>
	<div>
		<strong>本文链接：</strong>
		<a href="http://zzylxx.gitee.io/BLE 连接">
			http://zzylxx.gitee.io/BLE 连接
		</a>
	</div>
	<div>
		<span>欢迎任何形式的转载，但请务必注明出处。</span>
	</div>
	<div>
		<span>由于笔者水平有限，如果文章或代码有表述不当之处，还请不吝赐教。</span>
	</div>
</div>


<style>
	.post-copyright {
		background-color: #fff;
		/* box-shadow: 0px 0px 1px 1px rgba(0, 0, 0, 0.05); */
		box-shadow: 0 6px 30px -10px rgba(0, 0, 0, 0.1);
		display: block;
		margin: 20px auto;
		margin-bottom: 15px;
		padding: 10px;
		font-size: 15px;
		color: #2e2d2d;
		word-break: break-all;
		line-height: 1.8;
	}


	.post-copyright a {
		color: #2e2d2d;
		border-bottom: 1px solid #999;
	}

	.post-copyright a:hover {
		color: #2e2d2d;
		border-bottom: 1px solid #000;
	}
</style>

    <!-- comments -->
    <div class="comments">
    <h3 id="commenttitle" style="margin-top: 20px;margin-left: 10px;">评论区</h3>
    <div id="tcomment"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/twikoo@1.0.0/dist/twikoo.all.min.js"></script>
<script>twikoo.init({ envId: 'twikoo-1gx61ki809c44dc7', el: '#tcomment' })</script>

    <div class="right-icon">
      <div id="to-valine" class="right-icon-item">
        <a href="#commenttitle">评论</a>
      </div>
    </div>

  </div>


  <aside>
    <div class="aside-wrap">
    
        <section class="widget-wrap">
    <h3 class="widget-title">简介</h3>
    <div class="widget">
        <div class="notice">
            <img class="notice-img" src="/image/head_portrait.jpg">
            <p>路漫漫其修远兮，<br>吾将上下而求索!<br></p>
            
            <p class="notice-item">
                <!-- github -->
                <span><a target="_blank" href="https://github.com/qgyhd1234"><img src="https://shuxhan-imgbed.oss-cn-hangzhou.aliyuncs.com/img/20210108165043.jpg" alt="Github"></a></span>
                <!-- juejin -->
                <!-- <span><a target="_blank" href=""><img src="https://shuxhan-imgbed.oss-cn-hangzhou.aliyuncs.com/img/20210108165800.png" alt="掘金社区"></a></span> -->
                <!-- zhihu -->
                <!-- <span><a target="_blank" href=""><img src="https://shuxhan-imgbed.oss-cn-hangzhou.aliyuncs.com/img/20210108172745.ico" alt="知乎"></a></span> -->
                <!-- csdn -->
                <!-- <span><a target="_blank" href=""><img src="https://shuxhan-imgbed.oss-cn-hangzhou.aliyuncs.com/img/20210108172932.ico" alt="CSDN"></a></span> -->
            </p>

            <ul class="statistics">
                <li>
                    <span>文章</span>
                    <a href="./archives">7</a>
                </li>
                <li>
                    <span>字数</span>
                    <a>9.7k</a>
                </li>
                <li>
                    <span>访客</span>
                    <a id="busuanzi_value_site_uv"></a>
                </li>
                
                
            </ul>

            <div id="follow" class="button">
                <a target="_blank" href="https://github.com/shuxhan">关注</a>
            </div>
        </div>
    </div>
</section>

    
        
  <section class="widget-wrap">
    <h3 class="widget-title">文章分类</h3>
    <div class="widget">
      <div class="categories-list">
        
           
            
              <a class="category-list-item" href="/categories/BLE">
                <span class="category-list-link">BLE</span>
                <span class="category-list-number">(7)</span>
              </a>
            
          
        
      </div>
    </div>
  </section>


    
        
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/BLE/" rel="tag">BLE</a></li></ul>
    </div>
  </div>


    
        
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul class="recent-posts">
        
          <li>
            <a href="/2021/02/28/GAP%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE/">BLE GAP参数设置</a>
          </li>
        
          <li>
            <a href="/2021/02/28/BLE%E5%B9%BF%E6%92%AD%E5%92%8C%E6%89%AB%E6%8F%8F%E5%93%8D%E5%BA%94/">BLE 广播和扫描响应</a>
          </li>
        
          <li>
            <a href="/2021/02/28/BLE%20%E8%BF%9E%E6%8E%A5/">BLE 连接</a>
          </li>
        
          <li>
            <a href="/2021/02/28/BLE%20SM/">BLE Security Manager</a>
          </li>
        
          <li>
            <a href="/2021/02/28/BLE%20GATT/">BLE GATT</a>
          </li>
        
      </ul>
    </div>
  </div>

    
        
  <section class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/02/">二月 2021</a></li></ul>
    </div>
  </section>


    
    
</div>


    <!-- 目录 -->
    
  </aside>
  
</div>

					</div>
				</div>

				<!-- 返回顶部按钮 -->
				<div class="right-icon">
					<div id="to-top" class="right-icon-item">
						<a>
							<span class="top-left"></span>
							<span class="top-right"></span>
						</a>
					</div>
				</div>
		</div>
		<footer class="footer">

    <div class="copyright-wrap">
      <span>&copy; 2021-2021 朝雨绫萧</span>
      <span id="timeDate">载入天数...</span><span id="busuanzi_container_site_pv">
      总访问量<span id="busuanzi_value_site_pv"></span>次
    </span>
    </div>
    
    
    <!-- <span id="busuanzi_container_site_uv">
      ，本站访客数<span id="busuanzi_value_site_uv"></span>人次
    </span> -->
    
</footer>

<script>
    var now = new Date();
    function createtime() {
      var grt = new Date("2/27/2021 16:12:00");//此处修改你的建站时间或者网站上线时间 
      now.setTime(now.getTime() + 250);
      days = (now - grt) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
      document.getElementById("timeDate").innerHTML = "本站已安全运行 " + dnum + " 天 ";
    }
    setInterval("createtime()", 1000);
</script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<script src="/lib/jquery.js"></script>


<script src="/lib/waves.js"></script>


<script src="/lib/jquery-ui.js"></script>


<script src="/lib/jquery.tocify.js"></script>


<script src="/js/main.js"></script>


			
<style>
  .copy-btn {
    display: inline-block;
    padding: 6px 12px;
    font-size: 13px;
    font-weight: 500;
    line-height: 20px;
    color: rgb(206, 206, 206);
    white-space: nowrap;
    vertical-align: middle;
    cursor: pointer;
    border-radius: 3px;
    user-select: none;
    outline: 0;
  }

  .copy-btn:hover {
    color: #888
  }

  .highlight-wrap .copy-btn {
    padding: 2px 6px;
    position: absolute;
    right: 10px;
    top: 1px;
    z-index: 1;
  }



  .highlight-wrap {
    position: relative;
  }

  #copysuccess {
    width: 200px;
    height: 75px;
    position: fixed;
    right: 50%;
    margin-right: -100px;
    top: 40%;
    margin-top: -40px;
    color: #fff;
    font-weight: 600;
    background: #444444d8;
    display: none;
    z-index: 99999;
    border-radius: 10px;
    text-align: center;
    line-height: 30px;
    padding-top: 10px;
    user-select: none;
  }
</style>

<script>
  addLoadEvent(() => {
    $('.highlight').each(function (i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap')
      $(e).after($wrap)
      $wrap.append($('<div>').addClass('copy-btn').append('复制代码').on('click', function (e) {
        var copysuccess = document.getElementById('copysuccess');
        copysuccess.style.display = 'block'
        setTimeout(function () {
          copysuccess.style.display = 'none'
        }, 2000)
        var code = $(this).parent().find(".code")[0].innerText
          
                var ta = document.createElement('textarea')
        document.body.appendChild(ta)
        ta.style.position = 'absolute'
        ta.style.top = '0px'
        ta.style.left = '0px'
        ta.value = code
        ta.select()
        ta.focus()
        var result = document.execCommand('copy')
        document.body.removeChild(ta)
          
                  if (result) $(this).text('复制成功')
            else $(this).text('复制失败')
              
                $(this).blur()
      })).on('mouseleave', function (e) {
        var $b = $(this).find('.copy-btn')
        setTimeout(function () {
          $b.text('复制代码')
        }, 1000)
      }).append(e)
    })
  })
</script>

				<div id="copysuccess">复制成功<br>欢迎阅读文章~</div>
	</body>

</html>

<script>
	$('#to-top').click(function () { $('html,body').animate({ scrollTop: '0px' }, 500); return false; });
</script>